/**
 * @Method VeTable
 * @Description TODO
 * @Date 2021/1/20 14:59
 * @Created by HePing
 */
export default {
  name: 'VeTable',
  model: {
    prop: 'model'
  },
  props: {
    model: {},
    config: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  render(h) {
    const vm = this
    const {
      table,
      widget: { style, nativeOn, props, attrs, className }
    } = vm.config
    const tableData = Array.isArray(vm.model) ? vm.model : []
    const children = ({ columns = [], label = 'label', prop = 'prop' }) => {
      return columns.map(cell => {
        const opts = { ...cell, label: cell[label], prop: cell[prop] }
        const { formatter } = opts
        return h(
          'el-table-column',
          {
            props: opts,
            scopedSlots: {
              default(scope) {
                const { column: { property }, row } = scope
                if (typeof formatter === 'function') {
                  return formatter(h, row[property], scope)
                } else {
                  return row[property]
                }
              }
            }
          },
          vm.$slots.default
        )
      })
    }
    return h(
      'el-table',
      {
        attrs,
        style,
        props: {
          ...props,
          data: tableData
        },
        on: vm.$listeners,
        class: className
      },
      children(table)
    )
  }
}
